package org.doxla.oomwatch.observerable;

import java.math.BigDecimal;
import java.util.List;

public class PercentageFreeThresholdOomWatcher extends AbstractOomWatcher {
	
	public PercentageFreeThresholdOomWatcher() {
		super();
	}

	public PercentageFreeThresholdOomWatcher(List<OomObserver> obs, long l) {
		super(obs,l);
	}

	@Override
	public boolean thresholdExceeded() {
		BigDecimal maxMem = new BigDecimal(Runtime.getRuntime().maxMemory());
		BigDecimal freeMem = maxMem.subtract(new BigDecimal(Runtime.getRuntime().totalMemory()));
		BigDecimal percentageFree = freeMem.divide(maxMem,2,BigDecimal.ROUND_HALF_UP);
		percentageFree = percentageFree.multiply(BigDecimal.valueOf(100));
		
		return percentageFree.longValue() < getThreshold();
	}

}
